<template>
  <view class="main">
	 <view class="part1">
	   <view class="part2" v-if="permisson==1">
		<view class="part2-1">
		 优选作品添加
		 <view class="ptxt1">{{ptxt1}}</view>
		</view> 
		<view class="part2-2">
		  <view class="ptxt2">{{ptxt}}</view>
		  <image src="https://ppp.new.zeroitem.cn/ppp/add.png" class="pimg30" @click="uploadvideo"></image>
	    </view> 
	   </view>
	   
	  <view class="part2" v-if="permisson==0">
	  		<view class="part2-1">
	  		 优先作品展示
	  		 <view class="ptxt1" style="color:black">拍摄同款</view>
	  		</view> 
	  		<view class="part2-2">
	  		  <view class="ptxt2">点击右边，选择拍摄同款</view>
	  		  <image src="https://ppp.new.zeroitem.cn/ppp/add.png" class="pimg30" @click="pth(zid)"></image>
	   </view> 
	  </view> 
	   
	   
	   
	   
		<view class="partvideo ap3">
		   <video :src="video_src" style="width:654rpx;height:250rpx;" objectFit="cover" class="pimg17" :poster="video_pic" v-if="video_src!=''"></video>
		   <image :src="video_pic" style="width:654rpx;height:250rpx;" class="pimg17" v-if="video_src==''"></image>
		</view> 	   
	   
	   
	  <view class="part3">
		 <view class="part3-1" style="margin-top:24rpx;">
		    <view class="p1">作品名称</view>
			<view class="p2">
		      <input type="text" placeholder="请输入优选作品名称" class="cinput" v-model="zpmc"/>
			</view>
		 </view> 
		 
		 <view class="part3-1" style="justify-content: flex-start;margin-top:10rpx;padding-left:10rpx"> 参考价格</view> 
		 <view class="part3-1" style="margin-top:2rpx">
			<view class="p2" style="display:flex;justify-content: space-between;">
		       <view class="price" :class="{ cur:ptab==1}" @click="sptab(1)">￥388</view>
			   <view class="price" :class="{ cur:ptab==2}" @click="sptab(2)">￥699</view>
			   <view class="price" :class="{ cur:ptab==3}" @click="sptab(3)">￥999</view>
			   <view class="price" :class="{ cur:ptab==4}" @click="sptab(4)">其他</view>
			   <input type="text" placeholder="其他价格" class="cinput" v-model="qtjg" style="width:120rpx;height:50rpx;margin-left:10rpx;padding-left:2rpx" v-if="ptab==4"/>
			</view>
		 </view> 
		 <view class="part3-1" style="color:red;font-size:28rpx;margin-top:24rpx">
			 ￥388---对应拍摄时长为半天;
			 ￥699---对应拍摄时长为全天;
			 ￥999---对应拍摄时长为超全天;
			 其他价格---统一的拍摄时长为超全天;
		  </view>
			 
		 <view class="part3-1" v-if="permisson==1">
			<view class="dw" @click="dingwei">
			  定位
			 <image src="https://ppp.new.zeroitem.cn/ppp/dw.png" class="img3"></image>
			</view>
			<view class="p2">
		      <input type="text" placeholder="请定位拍摄地址" class="cinput" v-model="address" disabled="true"/>
			</view>
		 </view> 
		
		<view class="part3-1" v-if="permisson==0">
		  {{address}}
		</view> 
			 
		 
	  </view>		 
	 </view>  
	 <view class="part4">
		<view class="part4-1">
		  请选择作品标签
		</view>
		<view class="ptxt">
	    <view class="pitem" :class="{ cur: sitem.indexOf(i) != -1 }" v-for="(item,i) of pitem" :key="i" @click="appointment(i)">{{item.val}}</view>
		</view>
		<view class="btn" @click="submitorder" v-if="permisson==1">确认提交</view>
	 </view>
  </view>
</template>

<script>
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import {
		toLogin
	} from '@/utils/login'
	
	import {
		loadingFun,
		menuJump
	} from '@/utils/tools'	
    import {
		tjyx,zpinfo,plable
	} from '@/api/ppp'	
    import {
		loadingType
	} from '@/utils/type'
    import {
	  uploadFileToTencentClound
	} from '@/js_sdk/COS'
	const app = getApp()
	export default {
		data() {
			return {
		     zpmc:"",
			 pssj:"",
			 ckjg:"",
			 qtjg:"",
			 zid:0,
			 ptab:0,
			 permisson:1,
			 ptxt:"请添加作品参考片",
			 ptxt1:"待上传",
			 vname:"",
			 address:"",
			 latitude:"",
			 longitude:"",
			 video_src:"",
			 video_pic:"https://ppp.new.zeroitem.cn/uploads/1.gif",
			 pitem:[],
			 sitem:[]
           }
		},
		onLoad(options) {
		 if(options.zid!=undefined){
		  this.zid=options.zid;
		 }
		 this.getzinfo(this.zid);
		},
		onShow() {
         //this.showpitem();
		},

		onReachBottom() {
	    
		},
		onPullDownRefresh() {

		},

		methods: {
		pth(id){
		 wx.navigateTo({
		  url:"/pages/ppp/order/pthorder?id="+id
		 })	
		},	
		sptab(type){
		    this.ptab=type;
	        if(type==1){this.ckjg=388;this.pssj=1;} 
			if(type==2){this.ckjg=699;this.pssj=2;}
		    if(type==3){this.ckjg=999;this.pssj=3;}
			if(this==4){this.pssj=3;}
		},
		async showpitem(itemids){
		  const{
		   code,
		   msg,
		   data
		  }= await plable();
		  if(code==1){
		  	this.pitem=data;
			var knz=0;var sitem=[];
			if(itemids.length>0){
				for(var kz=0;kz<data.length;kz++){
				 var id=data[kz].id;
				 if(itemids.indexOf(id)!==-1){
				   sitem[knz]=kz;
				   knz++;
				 }
				}
			}
		    this.sitem=sitem;
		  }	 
		},
		async getzinfo(zid){
		 const{code,msg,data}= await zpinfo(zid); 
		 this.sitem=data.sitem;
		 this.showpitem(this.sitem);
		 if(zid>0){
		  this.zpmc=data.zpinfo.zpmc;
		  this.pssj=1;
		  this.ckjg=data.zpinfo.ckjg;
		  var ckjg=data.zpinfo.ckjg;;
		  if(ckjg==388){this.ptab=1;this.pssj=1;} 
		  else if(ckjg==699){this.ptab=2;this.pssj=2;} 
		  else if(ckjg==999){this.ptab=3;this.pssj=3;} 
		  else{this.ptab=4;this.qtjg=ckjg;}  
		  this.video_pic=data.zpinfo.video_pic;
		  this.video_src=data.zpinfo.video_src;
		  this.address=data.zpinfo.address;
		  this.latitude=data.zpinfo.latitude;
		  this.longitude=data.zpinfo.longitude;
		  this.permisson=data.permisson
		  }
		},
		async submitorder(){
		 var zpmc=this.zpmc;
		 var pssj=this.pssj;
		 var ptab=this.ptab;
		 if(this.ptab<=3){
		  var ckjg=this.ckjg;
		 }else{
		   var ckjg=this.qtjg;	 
		 }
		 var video_pic=this.video_pic;
		 var video_src=this.video_src;
		 var address=this.address;
		 var latitude=this.latitude;
		 var longitude=this.longitude;
		 var sitem=this.sitem;
		 var pitem=this.pitem;
		 if(zpmc==''){
		  uni.showToast({title:"请填写作品名称",icon:"none"}); 
		  return false;
		 }
		/***
		 if(pssj==''){
		  uni.showToast({title:"请填写拍摄时长",icon:"none"}); 
		  return false;
		 }	
		 */
	
		 if(ckjg==''){
			uni.showToast({title:"请填写拍摄的价格",icon:"none"}); 
			return false;
		 }	
		 if(video_src==''){
			uni.showToast({title:"请上传作品参考片",icon:"none"}); 
			return false;
		 }
		 if(address==''){
			uni.showToast({title:"请定位拍摄地址",icon:"none"}); 
			return false;
		 }				 	 
	     if(sitem.length==0){
		  uni.showToast({title:"请选择作品标签",icon:"none"});
		  return false;	 
		 }
		 var imp=[];
		 for(var kz=0;kz<sitem.length;kz++){
		    imp[kz]=pitem[sitem[kz]]['id'];	
		  }
		  var impstr=imp.join("|")
			var odata={
				zpmc:zpmc,video_pic:video_pic,
				video_src:video_src,
				address:address,
				latitude:latitude,
				longitude:longitude,
				ckjg:ckjg,
				zid:this.zid,
				ptab:ptab,
				sitem:impstr
			}; 
	       const{
				code,
				msg,
				data,
			  }= await tjyx(odata);
			  if(code==1){
				uni.navigateTo({
					url:"/pages/ppp/ps/main1?uid="+data.uid
				})  
			  }		 
		},
         appointment(index) {
         					let that = this;
         					if (that.sitem.indexOf(index) == -1 && that.sitem.length<4) {
         						// console.log(index); //打印下标
         						that.sitem.push(index); //选中添加到数组里
         					}else if(that.sitem.length==4 ||that.sitem.indexOf(index), 1){
         						uni.showToast({
         							title:'最多只能选三个',
         							duration:1000,
         							icon:'none'
         						})
         						that.sitem.splice(that.sitem.indexOf(index), 1); //取消
         					}
          },
				
		    uploadvideo(){
				 var that=this;
					uni.chooseMedia({
						count:1,
						mediaType:['video'],
						sourceType: ['camera', 'album'],
						success: function (res) {
						  let filePath =res.tempFiles[0].tempFilePath;
						  let ext = filePath.substr(filePath.lastIndexOf('.') + 1);
						  var timestamp = (new Date()).valueOf();
						  var filename=timestamp+"."+ext;
						  
						  let filePath1 =res.tempFiles[0].thumbTempFilePath;
						  let ext1 = filePath1.substr(filePath1.lastIndexOf('.') + 1);
						  var filename1=timestamp+"."+ext1;
						  uni.showLoading({title:"视频上传中",mask:true});
						  uploadFileToTencentClound(filename,filePath).then((res1)=>{
						  	
							//that.vname=filename;
							//that.vsrc='https://ppp-1300934138.cos.ap-shanghai.myqcloud.com/wxFile/'+filename;
						    var vsrc='https://ppp-1300934138.cos.ap-shanghai.myqcloud.com/wxFile/'+filename;
							uploadFileToTencentClound(filename1,filePath1).then((res1)=>{
						        that.ptxt1="已上传";
								that.ptxt="点击上传视频，可以更改视频";
							    var vsrc1='https://ppp-1300934138.cos.ap-shanghai.myqcloud.com/wxFile/'+filename1; 
								that.video_src=vsrc;
								that.video_pic=vsrc1;
								uni.hideLoading();
							})			
						  })
						 }
				    });  
				  },
		
		
					
					
		dingwei(){
			uni.chooseLocation({
			  success:(res)=> {
						  this.address=res.address;
						  this.latitude=res.latitude;
						  this.longitude=res.longitude;
						}
					});
				},
              fail(err) {  
                console.log('choose location fail:', err)  
              }, 
		},
		computed: {
			...mapGetters(['isLogin']),
		}
	}
</script>
<style>
 @import "./tjyx.css";	
</style>
